#include "BoidSpaceShip.h"

//! constructor.
BoidSpaceShip::BoidSpaceShip(OgreOde::Body *mBody, float maxVelMag, float forceMag,
	float reverseForceMag, Ogre::Radian maxTurning, Ogre::Radian maxRoll) :
BoidBase(mBody), mVelocity(0, 0, 0), mMaxVelMag(maxVelMag), mForceMag(forceMag),
mReverseForceMag(reverseForceMag), mMaxTurning(maxTurning), mMaxRoll(maxRoll)
{
}

//! steer boid base on desired steering vector.
void BoidSpaceShip::steerBoid(const Ogre::Vector3& desiredVector)
{
	// move ship.
	Ogre::Quaternion rot = getForwardVector().getRotationTo(desiredVector);
	Ogre::Radian angle; Ogre::Vector3 axis;
	rot.ToAngleAxis(angle, axis);
	//if (Ogre::Math::Abs(angle) > mMaxTurning) angle = mMaxTurning * Ogre::Math::Sign(angle);
	//mBody->rotate(axis, angle, Ogre::SceneNode::TS_PARENT);

	mVelocity += getForwardVector() * mForceMag;

	// clip speed.
	if (mVelocity.squaredLength() > mMaxVelMag * mMaxVelMag)
	{
		mVelocity -= mVelocity.normalisedCopy() * mReverseForceMag;

		// make sure we didn't over do it.
		if (mVelocity.squaredLength() < mMaxVelMag * mMaxVelMag)
		{
			mVelocity.normalise();
			mVelocity *= mMaxVelMag;
		}
	}

	//mBody->setPosition(mBody->getPosition() * mVelocity);
	mBody->setLinearVelocity(mBody->getOrientation() * mVelocity * 10);
}
